package com.example.mytest;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import java.io.File;


/**
 * 自定义Application
 * 
 * @author yuanyi
 *
 */
public class BaseApplication extends MultiDexApplication {
	public static String type = "s";	//s：车机版		c：手机版

	private static final String TAG = "JIGUANG-Example";

	public static int width0 = 1024;
	public static int height0 = 600;

	public static int width1 = 1024;
	public static int height1 = 600;

	public  static Context context;
	private static BaseApplication app;
	private String packageName = "";
	private PackageInfo packageInfo;
//	private IBLE bleManager;
	private boolean debug = false;
	private boolean test = false;
	private boolean testLog = false;
	public static int width;
	public static int height;
	public static float density;

	public static String rid;
	public static int id;

	public static String versionName;

	public static BaseApplication getInstance() {
		return app;
	}

//	@Override
//	public String getPackageName() {
//		return packageName;
//	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public PackageInfo getPackageInfo() {
		return packageInfo;
	}

	public void setPackageInfo(PackageInfo packageInfo) {
		this.packageInfo = packageInfo;
	}

	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}

	public boolean isTestLog() {
		return testLog;
	}

	public void setTestLog(boolean testLog) {
		this.testLog = testLog;
	}

	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		MultiDex.install(base);
	}

//	private void checkFix(){
//		try {
//			String dexPath = Environment.getExternalStorageDirectory() + "/classes2.dex";
//			HotFix.fixDexFile(this, dexPath);
//
//			Toast.makeText(this, "修复成功", Toast.LENGTH_SHORT).show();
//		} catch (Exception e) {
//			Toast.makeText(this, "修复失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
//			e.printStackTrace();
//		}
//	}

	@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
	@Override
	public void onCreate() {
		super.onCreate();

//		File dexPath = new File(getDir("dex", Context.MODE_PRIVATE), "fix.dex");
//		Log.e("mainHHH","dexPath = " + dexPath.getAbsolutePath());
//		Utils.prepareDex(getApplicationContext(), dexPath, "fix.dex");
//		HotFix.patch(getApplicationContext(), dexPath.getAbsolutePath(), "");

		HotFix.patch(getApplicationContext());

		app = this;

//		checkFix();

		Log.e("BaseApplication===0", "===");

//		MultiDex.install(this);

		//获取屏幕分辨率
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(metrics);

		width = metrics.widthPixels;

		wm.getDefaultDisplay().getRealMetrics(metrics);


		height = metrics.heightPixels;
		density = metrics.density;

		Log.e("BaseApplication===", density+"==="+width+"==="+height);

		//获取app包名
		try {
			packageName = getPackageName();

			packageInfo = getPackageManager().getPackageInfo(packageName, 0);

			versionName = packageInfo.versionName;

			Log.e("BaseApplication===1", getPackageName()+"==="+packageInfo.versionName);
		} catch (Exception e) {
			Log.e("BaseApplication===e", "==="+e);
			e.printStackTrace();
		}





	}


	// 各个平台的配置，建议放在全局Application或者程序入口
	{
		// 微信 wx12342956d1cab4f9,a5ae111de7d9ea137e88a5e02c07c94d
//		PlatformConfig.setWeixin("wx86d98ec252f67d07", "4e4aafa841609025036b0367d30ef052");
//		// QQ
//		PlatformConfig.setQQZone("1105975305", "wkfWoPW8aYGwdvcL");
	}

//	private static void initImageLoader(Context context) {
//
//		File cacheDir = StorageUtils.getCacheDirectory(context);
//
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
//				.memoryCacheExtraOptions(height0, 800) // maxwidth, max
//				// height，即保存的每个缓存文件的最大长宽
//				.threadPoolSize(3)// 线程池内加载的数量
//				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
//				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // 你可以通过自己的内存缓存实现
//				.memoryCacheSize(2 * 1024 * 1024).diskCacheSize(50 * 1024 * 1024)
//				.diskCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5加密
//				.tasksProcessingOrder(QueueProcessingType.LIFO).diskCacheFileCount(100) // 缓存的文件数量
//				.diskCache(new UnlimitedDiskCache(cacheDir))// 自定义缓存路径
//				.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
//				.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout(5s),readTimeout(30s)超时时间
//				.writeDebugLogs() // Remove for releaseapp
//				.build();// 开始构建
//
//		ImageLoader.getInstance().init(config);
//	}

//	// 注册App异常崩溃处理器
//	private void registerUncaughtExceptionHandler() {
//		Thread.setDefaultUncaughtExceptionHandler(AppException.getAppExceptionHandler());
//	}

//	@Override
//	public void onLowMemory() {
//		super.onLowMemory();
//		System.gc();
//	}
//
//	public IBLE getIBLE() {
//		if (bleManager == null) {
//
//			Log.e("main===", "IBLE====");
//
//			bleManager = new AndroidBle(this);
//		}
//		return bleManager;
//	}

}
